<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>open - Perldoc Browser</title>
    <link rel="search" href="https://perldoc.perl.org/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser">
    <link rel="canonical" href="https://perldoc.perl.org/functions/open">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/stackoverflow-light.min.css" integrity="sha512-cG1IdFxqipi3gqLmksLtuk13C+hBa57a6zpWxMeoY3Q9O6ooFxq50DayCdm0QrDgZjMUn23z/0PMZlgft7Yp5Q==" crossorigin="anonymous" />
    <style>
      body {
        background: #f4f4f5;
        color: #020202;
      }
      .navbar-dark {
        background-image: -webkit-linear-gradient(top, #005f85 0, #002e49 100%);
        background-image: -o-linear-gradient(top, #005f85 0, #002e49 100%);
        background-image: linear-gradient(to bottom, #005f85 0, #002e49 100%);
        filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff005f85', endColorstr='#ff002e49', GradientType=0);
        background-repeat: repeat-x;
      }
      .navbar-dark .navbar-nav .nav-link,
      .navbar-dark .navbar-nav .nav-link:focus { color: #fff }
      .navbar-dark .navbar-nav .nav-link:hover { color: #ffef68 }
      #wrapperlicious {
        margin: 0 auto;
        font: 0.9em 'Helvetica Neue', Helvetica, sans-serif;
        font-weight: normal;
        line-height: 1.5em;
        margin: 0;
        padding: 0;
      }
      #wrapperlicious h1 { font-size: 1.5em }
      #wrapperlicious h2 { font-size: 1.3em }
      #wrapperlicious h3 { font-size: 1.1em }
      #wrapperlicious h4 { font-size: 0.9em }
      #wrapperlicious h1, #wrapperlicious h2, #wrapperlicious h3,
      #wrapperlicious h4, #wrapperlicious dt {
        color: #020202;
        margin-top: 1em;
        margin-bottom: 1em;
        position: relative;
        font-weight: bold;
      }
      #wrapperlicious a { color: inherit; text-decoration: underline }
      #wrapperlicious #toc { text-decoration: none }
      #wrapperlicious a:hover { color: #2a2a2a }
      #wrapperlicious a img { border: 0 }
      #wrapperlicious :not(pre) > code {
        color: inherit;
        background-color: rgba(0, 0, 0, 0.04);
        border-radius: 3px;
        font: 0.9em Consolas, Menlo, Monaco, monospace;
        padding: 0.3em;
      }
      #wrapperlicious dd {
        margin: 0;
        margin-left: 2em;
      }
      #wrapperlicious dt {
        color: #2a2a2a;
        font-weight: bold;
        margin-left: 0.9em;
      }
      #wrapperlicious p {
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious li > p {
        margin-bottom: 0;
        margin-top: 0;
      }
      #wrapperlicious pre {
        border: 1px solid #c1c1c1;
        border-radius: 3px;
        font: 100% Consolas, Menlo, Monaco, monospace;
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious pre > code {
        display: block;
        background-color: #f6f6f6;
        font: 0.9em Consolas, Menlo, Monaco, monospace;
        line-height: 1.5em;
        text-align: left;
        white-space: pre;
        padding: 1em;
      }
      #wrapperlicious dl, #wrapperlicious ol, #wrapperlicious ul {
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious ul {
        list-style-type: square;
      }
      #wrapperlicious ul ul {
        margin-bottom: 0px;
        margin-top: 0px;
      }
      #footer {
        font-size: 0.8em;
        padding-top: 0.5em;
        text-align: center;
      }
      #more {
        display: inline;
        font-size: 0.8em;
      }
      #perldocdiv {
        background-color: #fff;
        border: 1px solid #c1c1c1;
        border-bottom-left-radius: 5px;
        border-bottom-right-radius: 5px;
        margin-left: auto;
        margin-right: auto;
        padding: 3em;
        padding-top: 1em;
        max-width: 960px;
      }
      #moduleversion { float: right }
      #wrapperlicious .leading-notice {
        font-style: italic;
        padding-left: 1em;
        margin-top: 1em;
        margin-bottom: 1em;
      }
      #wrapperlicious .permalink {
        display: none;
        left: -0.75em;
        position: absolute;
        padding-right: 0.25em;
        text-decoration: none;
      }
      #wrapperlicious h1:hover .permalink, #wrapperlicious h2:hover .permalink,
      #wrapperlicious h3:hover .permalink, #wrapperlicious h4:hover .permalink,
      #wrapperlicious dt:hover .permalink {
        display: block;
      }
    </style>
    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-KVNWBNT5FB"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());

      gtag('config', 'G-KVNWBNT5FB');
      gtag('config', 'UA-50555-3');
    </script>
  </head>
  <body>
    <nav class="navbar navbar-expand-md navbar-dark bg-dark justify-content-between">
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand" href="https://perldoc.perl.org/">Perldoc Browser</a>
  <div class="collapse navbar-collapse" id="navbarNav">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="open.html#" id="dropdownlink-stable" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">5.35.0</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-stable">
          <a class="dropdown-item" href="https://perldoc.perl.org/functions/open">Latest</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.2/functions/open">5.38.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.1/functions/open">5.38.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0/functions/open">5.38.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.3/functions/open">5.36.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.2/functions/open">5.36.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.1/functions/open">5.36.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.0/functions/open">5.36.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.3/functions/open">5.34.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.2/functions/open">5.34.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.1/functions/open">5.34.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.0/functions/open">5.34.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.32.1/functions/open">5.32.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.32.0/functions/open">5.32.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.3/functions/open">5.30.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.2/functions/open">5.30.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.1/functions/open">5.30.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.0/functions/open">5.30.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.3/functions/open">5.28.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.2/functions/open">5.28.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.1/functions/open">5.28.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.0/functions/open">5.28.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.3/functions/open">5.26.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.2/functions/open">5.26.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.1/functions/open">5.26.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.0/functions/open">5.26.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.4/functions/open">5.24.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.3/functions/open">5.24.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.2/functions/open">5.24.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.1/functions/open">5.24.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.0/functions/open">5.24.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.4/functions/open">5.22.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.3/functions/open">5.22.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.2/functions/open">5.22.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.1/functions/open">5.22.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.0/functions/open">5.22.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.3/functions/open">5.20.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.2/functions/open">5.20.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.1/functions/open">5.20.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.0/functions/open">5.20.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.4/functions/open">5.18.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.3/functions/open">5.18.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.2/functions/open">5.18.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.1/functions/open">5.18.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.0/functions/open">5.18.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.3/functions/open">5.16.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.2/functions/open">5.16.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.1/functions/open">5.16.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.0/functions/open">5.16.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.4/functions/open">5.14.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.3/functions/open">5.14.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.2/functions/open">5.14.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.1/functions/open">5.14.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.0/functions/open">5.14.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.5/functions/open">5.12.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.4/functions/open">5.12.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.3/functions/open">5.12.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.2/functions/open">5.12.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.1/functions/open">5.12.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.0/functions/open">5.12.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.10.1/functions/open">5.10.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.10.0/functions/open">5.10.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.9/functions/open">5.8.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.8/functions/open">5.8.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.7/functions/open">5.8.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.6/functions/open">5.8.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.5/functions/open">5.8.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.4/functions/open">5.8.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.3/functions/open">5.8.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.2/functions/open">5.8.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.1/functions/open">5.8.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.0/functions/open">5.8.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.2/functions/open">5.6.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.1/functions/open">5.6.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.0/functions/open">5.6.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_04/functions/open">5.005_04</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_03/functions/open">5.005_03</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_02/functions/open">5.005_02</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_01/functions/open">5.005_01</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005/functions/open">5.005</a>
        </div>
      </li>
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="open.html#" id="dropdownlink-dev" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dev</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-dev">
          <a class="dropdown-item" href="https://perldoc.perl.org/blead/functions/open">blead</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.6/functions/open">5.39.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.5/functions/open">5.39.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.4/functions/open">5.39.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.3/functions/open">5.39.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.2/functions/open">5.39.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.1/functions/open">5.39.1</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0-RC2/functions/open">5.38.0-RC2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0-RC1/functions/open">5.38.0-RC1</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.11/functions/open">5.37.11</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.10/functions/open">5.37.10</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.9/functions/open">5.37.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.8/functions/open">5.37.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.7/functions/open">5.37.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.6/functions/open">5.37.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.5/functions/open">5.37.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.4/functions/open">5.37.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.3/functions/open">5.37.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.2/functions/open">5.37.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.1/functions/open">5.37.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.0/functions/open">5.37.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.11/functions/open">5.35.11</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.10/functions/open">5.35.10</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.9/functions/open">5.35.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.8/functions/open">5.35.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.7/functions/open">5.35.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.6/functions/open">5.35.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.5/functions/open">5.35.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.4/functions/open">5.35.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.3/functions/open">5.35.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.2/functions/open">5.35.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.1/functions/open">5.35.1</a>
          <a class="dropdown-item active" href="open.html">5.35.0</a>
        </div>
      </li>
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="open.html#" id="dropdownlink-nav" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Documentation</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-nav">
          <a class="dropdown-item" href="../perl.html">Perl</a>
          <a class="dropdown-item" href="../perlintro.html">Intro</a>
          <a class="dropdown-item" href="../perl.html#Tutorials">Tutorials</a>
          <a class="dropdown-item" href="../perlfaq.html">FAQs</a>
          <a class="dropdown-item" href="../perl.html#Reference-Manual">Reference</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="../perlop.html">Operators</a>
          <a class="dropdown-item" href="../functions.html">Functions</a>
          <a class="dropdown-item" href="../variables.html">Variables</a>
          <a class="dropdown-item" href="../modules.html">Modules</a>
          <a class="dropdown-item" href="../perlutil.html">Utilities</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="../perlcommunity.html">Community</a>
          <a class="dropdown-item" href="../perlhist.html">History</a>
        </div>
      </li>
    </ul>
    <ul class="navbar-nav">
      <script>
        function set_expand (expand) {
          var perldocdiv = document.getElementById('perldocdiv');
          var width = window.getComputedStyle(perldocdiv).getPropertyValue('max-width');
          var expanded = (width == '' || width == 'none') ? true : false;
          if (expand === null) {
            expand = !expanded;
          }
          if ((expand && !expanded) || (!expand && expanded)) {
            perldocdiv.style.setProperty('max-width', expand ? 'none' : '960px');
            var button_classlist = document.getElementById('content-expand-button').classList;
            if (expand) {
              button_classlist.add('btn-light');
              button_classlist.remove('btn-outline-light');
            } else {
              button_classlist.add('btn-outline-light');
              button_classlist.remove('btn-light');
            }
          }
          return expand;
        }
        function toggle_expand () {
          var expand = set_expand(null);
          document.cookie = 'perldoc_expand=' + (expand ? 1 : 0) + '; path=/; expires=Tue, 19 Jan 2038 03:14:07 UTC';
        }
        function read_expand () {
          return document.cookie.split(';').some(function (item) { return item.indexOf('perldoc_expand=1') >= 0 });
        }
        if (document.readyState === 'loading') {
          document.addEventListener('DOMContentLoaded', function () {
            if (read_expand()) {
              set_expand(true);
            }
          });
        } else if (read_expand()) {
          set_expand(true);
        }
      </script>
      <button id="content-expand-button" type="button" class="btn btn-outline-light d-none d-lg-inline-block mr-2" onclick="toggle_expand()">Expand</button>
    <ul>
    <form class="form-inline" method="get" action="https://perldoc.perl.org/5.35.0/search">
      <input class="form-control mr-3" type="search" name="q" placeholder="Search" aria-label="Search">
    </form>
  </div>
</nav>

    <div id="wrapperlicious" class="container-fluid">
      <div id="perldocdiv">
        <div id="links">
          <a href="../functions.html">functions</a>
            / <a href="open.html">open</a>
          <div id="more">
            (<a href="open.txt">source</a>,
            <a href="CORE.html#open-FILEHANDLE,MODE,EXPR">CPAN</a>)
          </div>
        </div>
        <div class="leading-notice">
          You are viewing the version of this documentation from Perl 5.35.0.
            This is a development version of Perl.
        </div>
        <div class="leading-notice">
          <strong>open</strong> may also refer to the module: <a href="../open.html">open</a>
        </div>
      <dl>

<dt id="open-FILEHANDLE,MODE,EXPR"><a class="permalink" href="open.html#open-FILEHANDLE,MODE,EXPR">#</a><a id="open"></a><a id="open-FILEHANDLE-MODE-EXPR"></a>open FILEHANDLE,MODE,EXPR    </dt>
<dd>

</dd>
<dt id="open-FILEHANDLE,MODE,EXPR,LIST"><a class="permalink" href="open.html#open-FILEHANDLE,MODE,EXPR,LIST">#</a><a id="open1"></a><a id="open-FILEHANDLE-MODE-EXPR-LIST"></a>open FILEHANDLE,MODE,EXPR,LIST</dt>
<dd>

</dd>
<dt id="open-FILEHANDLE,MODE,REFERENCE"><a class="permalink" href="open.html#open-FILEHANDLE,MODE,REFERENCE">#</a><a id="open2"></a><a id="open-FILEHANDLE-MODE-REFERENCE"></a>open FILEHANDLE,MODE,REFERENCE</dt>
<dd>

</dd>
<dt id="open-FILEHANDLE,EXPR"><a class="permalink" href="open.html#open-FILEHANDLE,EXPR">#</a><a id="open3"></a><a id="open-FILEHANDLE-EXPR"></a>open FILEHANDLE,EXPR</dt>
<dd>

</dd>
<dt id="open-FILEHANDLE"><a class="permalink" href="open.html#open-FILEHANDLE">#</a><a id="open4"></a>open FILEHANDLE</dt>
<dd>

<p>Associates an internal FILEHANDLE with the external file specified by EXPR. That filehandle will subsequently allow you to perform I/O operations on that file, such as reading from it or writing to it.</p>

<p>Instead of a filename, you may specify an external command (plus an optional argument list) or a scalar reference, in order to open filehandles on commands or in-memory scalars, respectively.</p>

<p>A thorough reference to <code>open</code> follows. For a gentler introduction to the basics of <code>open</code>, see also the <a href="../perlopentut.html">perlopentut</a> manual page.</p>

<dl>

<dt id="Working-with-files"><a class="permalink" href="open.html#Working-with-files">#</a><a id="Working"></a>Working with files</dt>
<dd>

<p>Most often, <code>open</code> gets invoked with three arguments: the required FILEHANDLE (usually an empty scalar variable), followed by MODE (usually a literal describing the I/O mode the filehandle will use), and then the filename that the new filehandle will refer to.</p>

<dl>

<dt id="Simple-examples"><a class="permalink" href="open.html#Simple-examples">#</a><a id="Simple"></a>Simple examples</dt>
<dd>

<p>Reading from a file:</p>

<pre><code>open(my $fh, &quot;&lt;&quot;, &quot;input.txt&quot;)
    or die &quot;Can&#39;t open &lt; input.txt: $!&quot;;

# Process every line in input.txt
while (my $line = &lt;$fh&gt;) {
    #
    # ... do something interesting with $line here ...
    #
}</code></pre>

<p>or writing to one:</p>

<pre><code>open(my $fh, &quot;&gt;&quot;, &quot;output.txt&quot;)
    or die &quot;Can&#39;t open &gt; output.txt: $!&quot;;

print $fh &quot;This line gets printed into output.txt.\n&quot;;</code></pre>

<p>For a summary of common filehandle operations such as these, see <a href="../perlintro.html#Files-and-I%2FO">&quot;Files and I/O&quot; in perlintro</a>.</p>

</dd>
<dt id="About-filehandles"><a class="permalink" href="open.html#About-filehandles">#</a><a id="About"></a>About filehandles</dt>
<dd>

<p>The first argument to <code>open</code>, labeled FILEHANDLE in this reference, is usually a scalar variable. (Exceptions exist, described in &quot;Other considerations&quot;, below.) If the call to <code>open</code> succeeds, then the expression provided as FILEHANDLE will get assigned an open <i>filehandle</i>. That filehandle provides an internal reference to the specified external file, conveniently stored in a Perl variable, and ready for I/O operations such as reading and writing.</p>

</dd>
<dt id="About-modes"><a class="permalink" href="open.html#About-modes">#</a><a id="About1"></a>About modes</dt>
<dd>

<p>When calling <code>open</code> with three or more arguments, the second argument -- labeled MODE here -- defines the <i>open mode</i>. MODE is usually a literal string comprising special characters that define the intended I/O role of the filehandle being created: whether it&#39;s read-only, or read-and-write, and so on.</p>

<p>If MODE is <code>&lt;</code>, the file is opened for input (read-only). If MODE is <code>&gt;</code>, the file is opened for output, with existing files first being truncated (&quot;clobbered&quot;) and nonexisting files newly created. If MODE is <code>&gt;&gt;</code>, the file is opened for appending, again being created if necessary.</p>

<p>You can put a <code>+</code> in front of the <code>&gt;</code> or <code>&lt;</code> to indicate that you want both read and write access to the file; thus <code>+&lt;</code> is almost always preferred for read/write updates--the <code>+&gt;</code> mode would clobber the file first. You can&#39;t usually use either read-write mode for updating textfiles, since they have variable-length records. See the <b>-i</b> switch in <a href="../perlrun.html#-i%5Bextension%5D">perlrun</a> for a better approach. The file is created with permissions of <code>0666</code> modified by the process&#39;s <a href="umask.html"><code>umask</code></a> value.</p>

<p>These various prefixes correspond to the <a href="http://man.he.net/man3/fopen">fopen(3)</a> modes of <code>r</code>, <code>r+</code>, <code>w</code>, <code>w+</code>, <code>a</code>, and <code>a+</code>.</p>

<p>More examples of different modes in action:</p>

<pre><code># Open a file for concatenation
open(my $log, &quot;&gt;&gt;&quot;, &quot;/usr/spool/news/twitlog&quot;)
    or warn &quot;Couldn&#39;t open log file; discarding input&quot;;

# Open a file for reading and writing
open(my $dbase, &quot;+&lt;&quot;, &quot;dbase.mine&quot;)
    or die &quot;Can&#39;t open &#39;dbase.mine&#39; for update: $!&quot;;</code></pre>

</dd>
<dt id="Checking-the-return-value"><a class="permalink" href="open.html#Checking-the-return-value">#</a><a id="Checking"></a>Checking the return value</dt>
<dd>

<p>Open returns nonzero on success, the undefined value otherwise. If the <code>open</code> involved a pipe, the return value happens to be the pid of the subprocess.</p>

<p>When opening a file, it&#39;s seldom a good idea to continue if the request failed, so <code>open</code> is frequently used with <a href="die.html"><code>die</code></a>. Even if you want your code to do something other than <code>die</code> on a failed open, you should still always check the return value from opening a file.</p>

</dd>
</dl>

</dd>
<dt id="Specifying-I/O-layers-in-MODE"><a class="permalink" href="open.html#Specifying-I/O-layers-in-MODE">#</a><a id="Specifying"></a><a id="Specifying-I-O-layers-in-MODE"></a>Specifying I/O layers in MODE</dt>
<dd>

<p>You can use the three-argument form of open to specify I/O layers (sometimes referred to as &quot;disciplines&quot;) to apply to the new filehandle. These affect how the input and output are processed (see <a href="../open.html">open</a> and <a href="../PerlIO.html">PerlIO</a> for more details). For example:</p>

<pre><code>open(my $fh, &quot;&lt;:encoding(UTF-8)&quot;, $filename)
    || die &quot;Can&#39;t open UTF-8 encoded $filename: $!&quot;;</code></pre>

<p>This opens the UTF8-encoded file containing Unicode characters; see <a href="../perluniintro.html">perluniintro</a>. Note that if layers are specified in the three-argument form, then default layers stored in <a href="../perlvar.html#%24%7B%5EOPEN%7D"><code>${^OPEN}</code></a> (usually set by the <a href="../open.html">open</a> pragma or the switch <code>-CioD</code>) are ignored. Those layers will also be ignored if you specify a colon with no name following it. In that case the default layer for the operating system (:raw on Unix, :crlf on Windows) is used.</p>

<p>On some systems (in general, DOS- and Windows-based systems) <a href="binmode.html"><code>binmode</code></a> is necessary when you&#39;re not working with a text file. For the sake of portability it is a good idea always to use it when appropriate, and never to use it when it isn&#39;t appropriate. Also, people can set their I/O to be by default UTF8-encoded Unicode, not bytes.</p>

</dd>
<dt id="Using-undef-for-temporary-files"><a class="permalink" href="open.html#Using-undef-for-temporary-files">#</a><a id="Using"></a>Using <code>undef</code> for temporary files</dt>
<dd>

<p>As a special case the three-argument form with a read/write mode and the third argument being <a href="undef.html"><code>undef</code></a>:</p>

<pre><code>open(my $tmp, &quot;+&gt;&quot;, undef) or die ...</code></pre>

<p>opens a filehandle to a newly created empty anonymous temporary file. (This happens under any mode, which makes <code>+&gt;</code> the only useful and sensible mode to use.) You will need to <a href="seek.html"><code>seek</code></a> to do the reading.</p>

</dd>
<dt id="Opening-a-filehandle-into-an-in-memory-scalar"><a class="permalink" href="open.html#Opening-a-filehandle-into-an-in-memory-scalar">#</a><a id="Opening"></a>Opening a filehandle into an in-memory scalar</dt>
<dd>

<p>You can open filehandles directly to Perl scalars instead of a file or other resource external to the program. To do so, provide a reference to that scalar as the third argument to <code>open</code>, like so:</p>

<pre><code>open(my $memory, &quot;&gt;&quot;, \$var)
    or die &quot;Can&#39;t open memory file: $!&quot;;
print $memory &quot;foo!\n&quot;;    # output will appear in $var</code></pre>

<p>To (re)open <code>STDOUT</code> or <code>STDERR</code> as an in-memory file, close it first:</p>

<pre><code>    close STDOUT;
    open(STDOUT, &quot;&gt;&quot;, \$variable)
	or die &quot;Can&#39;t open STDOUT: $!&quot;;</code></pre>

<p>The scalars for in-memory files are treated as octet strings: unless the file is being opened with truncation the scalar may not contain any code points over 0xFF.</p>

<p>Opening in-memory files <i>can</i> fail for a variety of reasons. As with any other <code>open</code>, check the return value for success.</p>

<p><i>Technical note</i>: This feature works only when Perl is built with PerlIO -- the default, except with older (pre-5.16) Perl installations that were configured to not include it (e.g. via <code>Configure -Uuseperlio</code>). You can see whether your Perl was built with PerlIO by running <code>perl -V:useperlio</code>. If it says <code>&#39;define&#39;</code>, you have PerlIO; otherwise you don&#39;t.</p>

<p>See <a href="../perliol.html">perliol</a> for detailed info on PerlIO.</p>

</dd>
<dt id="Opening-a-filehandle-into-a-command"><a class="permalink" href="open.html#Opening-a-filehandle-into-a-command">#</a><a id="Opening1"></a>Opening a filehandle into a command</dt>
<dd>

<p>If MODE is <code>|-</code>, then the filename is interpreted as a command to which output is to be piped, and if MODE is <code>-|</code>, the filename is interpreted as a command that pipes output to us. In the two-argument (and one-argument) form, one should replace dash (<code>-</code>) with the command. See <a href="../perlipc.html#Using-open%28%29-for-IPC">&quot;Using open() for IPC&quot; in perlipc</a> for more examples of this. (You are not allowed to <a href="open.html"><code>open</code></a> to a command that pipes both in <i>and</i> out, but see <a href="../IPC::Open2.html">IPC::Open2</a>, <a href="../IPC::Open3.html">IPC::Open3</a>, and <a href="../perlipc.html#Bidirectional-Communication-with-Another-Process">&quot;Bidirectional Communication with Another Process&quot; in perlipc</a> for alternatives.)</p>

<pre><code>open(my $article_fh, &quot;-|&quot;, &quot;caesar &lt;$article&quot;)  # decrypt
                                                # article
    or die &quot;Can&#39;t start caesar: $!&quot;;

open(my $article_fh, &quot;caesar &lt;$article |&quot;)      # ditto
    or die &quot;Can&#39;t start caesar: $!&quot;;

open(my $out_fh, &quot;|-&quot;, &quot;sort &gt;Tmp$$&quot;)    # $$ is our process id
    or die &quot;Can&#39;t start sort: $!&quot;;</code></pre>

<p>In the form of pipe opens taking three or more arguments, if LIST is specified (extra arguments after the command name) then LIST becomes arguments to the command invoked if the platform supports it. The meaning of <a href="open.html"><code>open</code></a> with more than three arguments for non-pipe modes is not yet defined, but experimental &quot;layers&quot; may give extra LIST arguments meaning.</p>

<p>If you open a pipe on the command <code>-</code> (that is, specify either <code>|-</code> or <code>-|</code> with the one- or two-argument forms of <a href="open.html"><code>open</code></a>), an implicit <a href="fork.html"><code>fork</code></a> is done, so <a href="open.html"><code>open</code></a> returns twice: in the parent process it returns the pid of the child process, and in the child process it returns (a defined) <code>0</code>. Use <code>defined($pid)</code> or <code>//</code> to determine whether the open was successful.</p>

<p>For example, use either</p>

<pre><code>my $child_pid = open(my $from_kid, &quot;-|&quot;)
     // die &quot;Can&#39;t fork: $!&quot;;</code></pre>

<p>or</p>

<pre><code>my $child_pid = open(my $to_kid,   &quot;|-&quot;)
     // die &quot;Can&#39;t fork: $!&quot;;</code></pre>

<p>followed by</p>

<pre><code>    if ($child_pid) {
	# am the parent:
	# either write $to_kid or else read $from_kid
	...
       waitpid $child_pid, 0;
    } else {
	# am the child; use STDIN/STDOUT normally
	...
	exit;
    }</code></pre>

<p>The filehandle behaves normally for the parent, but I/O to that filehandle is piped from/to the STDOUT/STDIN of the child process. In the child process, the filehandle isn&#39;t opened--I/O happens from/to the new STDOUT/STDIN. Typically this is used like the normal piped open when you want to exercise more control over just how the pipe command gets executed, such as when running setuid and you don&#39;t want to have to scan shell commands for metacharacters.</p>

<p>The following blocks are more or less equivalent:</p>

<pre><code>open(my $fh, &quot;|tr &#39;[a-z]&#39; &#39;[A-Z]&#39;&quot;);
open(my $fh, &quot;|-&quot;, &quot;tr &#39;[a-z]&#39; &#39;[A-Z]&#39;&quot;);
open(my $fh, &quot;|-&quot;) || exec &#39;tr&#39;, &#39;[a-z]&#39;, &#39;[A-Z]&#39;;
open(my $fh, &quot;|-&quot;, &quot;tr&quot;, &#39;[a-z]&#39;, &#39;[A-Z]&#39;);

open(my $fh, &quot;cat -n &#39;$file&#39;|&quot;);
open(my $fh, &quot;-|&quot;, &quot;cat -n &#39;$file&#39;&quot;);
open(my $fh, &quot;-|&quot;) || exec &quot;cat&quot;, &quot;-n&quot;, $file;
open(my $fh, &quot;-|&quot;, &quot;cat&quot;, &quot;-n&quot;, $file);</code></pre>

<p>The last two examples in each block show the pipe as &quot;list form&quot;, which is not yet supported on all platforms. (If your platform has a real <a href="fork.html"><code>fork</code></a>, such as Linux and macOS, you can use the list form; it also works on Windows with Perl 5.22 or later.) You would want to use the list form of the pipe so you can pass literal arguments to the command without risk of the shell interpreting any shell metacharacters in them. However, this also bars you from opening pipes to commands that intentionally contain shell metacharacters, such as:</p>

<pre><code>open(my $fh, &quot;|cat -n | expand -4 | lpr&quot;)
	|| die &quot;Can&#39;t open pipeline to lpr: $!&quot;;</code></pre>

<p>See <a href="../perlipc.html#Safe-Pipe-Opens">&quot;Safe Pipe Opens&quot; in perlipc</a> for more examples of this.</p>

</dd>
<dt id="Duping-filehandles"><a class="permalink" href="open.html#Duping-filehandles">#</a><a id="Duping"></a>Duping filehandles</dt>
<dd>

<p>You may also, in the Bourne shell tradition, specify an EXPR beginning with <code>&gt;&amp;</code>, in which case the rest of the string is interpreted as the name of a filehandle (or file descriptor, if numeric) to be duped (as in <a href="http://man.he.net/man2/dup">dup(2)</a>) and opened. You may use <code>&amp;</code> after <code>&gt;</code>, <code>&gt;&gt;</code>, <code>&lt;</code>, <code>+&gt;</code>, <code>+&gt;&gt;</code>, and <code>+&lt;</code>. The mode you specify should match the mode of the original filehandle. (Duping a filehandle does not take into account any existing contents of IO buffers.) If you use the three-argument form, then you can pass either a number, the name of a filehandle, or the normal &quot;reference to a glob&quot;.</p>

<p>Here is a script that saves, redirects, and restores <code>STDOUT</code> and <code>STDERR</code> using various methods:</p>

<pre><code>#!/usr/bin/perl
open(my $oldout, &quot;&gt;&amp;STDOUT&quot;)
    or die &quot;Can&#39;t dup STDOUT: $!&quot;;
open(OLDERR,     &quot;&gt;&amp;&quot;, \*STDERR)
    or die &quot;Can&#39;t dup STDERR: $!&quot;;

open(STDOUT, &#39;&gt;&#39;, &quot;foo.out&quot;)
    or die &quot;Can&#39;t redirect STDOUT: $!&quot;;
open(STDERR, &quot;&gt;&amp;STDOUT&quot;)
    or die &quot;Can&#39;t dup STDOUT: $!&quot;;

select STDERR; $| = 1;  # make unbuffered
select STDOUT; $| = 1;  # make unbuffered

print STDOUT &quot;stdout 1\n&quot;;  # this works for
print STDERR &quot;stderr 1\n&quot;;  # subprocesses too

open(STDOUT, &quot;&gt;&amp;&quot;, $oldout)
    or die &quot;Can&#39;t dup \$oldout: $!&quot;;
open(STDERR, &quot;&gt;&amp;OLDERR&quot;)
    or die &quot;Can&#39;t dup OLDERR: $!&quot;;

print STDOUT &quot;stdout 2\n&quot;;
print STDERR &quot;stderr 2\n&quot;;</code></pre>

<p>If you specify <code>&#39;&lt;&amp;=X&#39;</code>, where <code>X</code> is a file descriptor number or a filehandle, then Perl will do an equivalent of C&#39;s <a href="http://man.he.net/man3/fdopen">fdopen(3)</a> of that file descriptor (and not call <a href="http://man.he.net/man2/dup">dup(2)</a>); this is more parsimonious of file descriptors. For example:</p>

<pre><code># open for input, reusing the fileno of $fd
open(my $fh, &quot;&lt;&amp;=&quot;, $fd)</code></pre>

<p>or</p>

<pre><code>open(my $fh, &quot;&lt;&amp;=$fd&quot;)</code></pre>

<p>or</p>

<pre><code># open for append, using the fileno of $oldfh
open(my $fh, &quot;&gt;&gt;&amp;=&quot;, $oldfh)</code></pre>

<p>Being parsimonious on filehandles is also useful (besides being parsimonious) for example when something is dependent on file descriptors, like for example locking using <a href="flock.html"><code>flock</code></a>. If you do just <code>open(my $A, &quot;&gt;&gt;&amp;&quot;, $B)</code>, the filehandle <code>$A</code> will not have the same file descriptor as <code>$B</code>, and therefore <code>flock($A)</code> will not <code>flock($B)</code> nor vice versa. But with <code>open(my $A, &quot;&gt;&gt;&amp;=&quot;, $B)</code>, the filehandles will share the same underlying system file descriptor.</p>

<p>Note that under Perls older than 5.8.0, Perl uses the standard C library&#39;s&#39; <a href="http://man.he.net/man3/fdopen">fdopen(3)</a> to implement the <code>=</code> functionality. On many Unix systems, <a href="http://man.he.net/man3/fdopen">fdopen(3)</a> fails when file descriptors exceed a certain value, typically 255. For Perls 5.8.0 and later, PerlIO is (most often) the default.</p>

</dd>
<dt id="Legacy-usage"><a class="permalink" href="open.html#Legacy-usage">#</a><a id="Legacy"></a>Legacy usage</dt>
<dd>

<p>This section describes ways to call <code>open</code> outside of best practices; you may encounter these uses in older code. Perl does not consider their use deprecated, exactly, but neither is it recommended in new code, for the sake of clarity and readability.</p>

<dl>

<dt id="Specifying-mode-and-filename-as-a-single-argument"><a class="permalink" href="open.html#Specifying-mode-and-filename-as-a-single-argument">#</a><a id="Specifying1"></a>Specifying mode and filename as a single argument</dt>
<dd>

<p>In the one- and two-argument forms of the call, the mode and filename should be concatenated (in that order), preferably separated by white space. You can--but shouldn&#39;t--omit the mode in these forms when that mode is <code>&lt;</code>. It is safe to use the two-argument form of <a href="open.html"><code>open</code></a> if the filename argument is a known literal.</p>

<pre><code>open(my $dbase, &quot;+&lt;dbase.mine&quot;)          # ditto
    or die &quot;Can&#39;t open &#39;dbase.mine&#39; for update: $!&quot;;</code></pre>

<p>In the two-argument (and one-argument) form, opening <code>&lt;-</code> or <code>-</code> opens STDIN and opening <code>&gt;-</code> opens STDOUT.</p>

<p>New code should favor the three-argument form of <code>open</code> over this older form. Declaring the mode and the filename as two distinct arguments avoids any confusion between the two.</p>

</dd>
<dt id="Calling-open-with-one-argument-via-global-variables"><a class="permalink" href="open.html#Calling-open-with-one-argument-via-global-variables">#</a><a id="Calling"></a>Calling <code>open</code> with one argument via global variables</dt>
<dd>

<p>As a shortcut, a one-argument call takes the filename from the global scalar variable of the same name as the filehandle:</p>

<pre><code>$ARTICLE = 100;
open(ARTICLE)
    or die &quot;Can&#39;t find article $ARTICLE: $!\n&quot;;</code></pre>

<p>Here <code>$ARTICLE</code> must be a global (package) scalar variable - not one declared with <a href="my.html"><code>my</code></a> or <a href="state.html"><code>state</code></a>.</p>

</dd>
<dt id="Assigning-a-filehandle-to-a-bareword"><a class="permalink" href="open.html#Assigning-a-filehandle-to-a-bareword">#</a><a id="Assigning"></a>Assigning a filehandle to a bareword</dt>
<dd>

<p>An older style is to use a bareword as the filehandle, as</p>

<pre><code>open(FH, &quot;&lt;&quot;, &quot;input.txt&quot;)
   or die &quot;Can&#39;t open &lt; input.txt: $!&quot;;</code></pre>

<p>Then you can use <code>FH</code> as the filehandle, in <code>close FH</code> and <code>&lt;FH&gt;</code> and so on. Note that it&#39;s a global variable, so this form is not recommended when dealing with filehandles other than Perl&#39;s built-in ones (e.g. STDOUT and STDIN).</p>

</dd>
</dl>

</dd>
<dt id="Other-considerations"><a class="permalink" href="open.html#Other-considerations">#</a><a id="Other"></a>Other considerations</dt>
<dd>

<dl>

<dt id="Automatic-filehandle-closure"><a class="permalink" href="open.html#Automatic-filehandle-closure">#</a><a id="Automatic"></a>Automatic filehandle closure</dt>
<dd>

<p>The filehandle will be closed when its reference count reaches zero. If it is a lexically scoped variable declared with <a href="my.html"><code>my</code></a>, that usually means the end of the enclosing scope. However, this automatic close does not check for errors, so it is better to explicitly close filehandles, especially those used for writing:</p>

<pre><code>close($handle)
   || warn &quot;close failed: $!&quot;;</code></pre>

</dd>
<dt id="Automatic-pipe-flushing"><a class="permalink" href="open.html#Automatic-pipe-flushing">#</a><a id="Automatic1"></a>Automatic pipe flushing</dt>
<dd>

<p>Perl will attempt to flush all files opened for output before any operation that may do a fork, but this may not be supported on some platforms (see <a href="../perlport.html">perlport</a>). To be safe, you may need to set <a href="../perlvar.html#%24%7C"><code>$|</code></a> (<code>$AUTOFLUSH</code> in <a href="../English.html">English</a>) or call the <code>autoflush</code> method of <a href="../IO::Handle.html#METHODS"><code>IO::Handle</code></a> on any open handles.</p>

<p>On systems that support a close-on-exec flag on files, the flag will be set for the newly opened file descriptor as determined by the value of <a href="../perlvar.html#%24%5EF"><code>$^F</code></a>. See <a href="../perlvar.html#%24%5EF">&quot;$^F&quot; in perlvar</a>.</p>

<p>Closing any piped filehandle causes the parent process to wait for the child to finish, then returns the status value in <a href="../perlvar.html#%24%3F"><code>$?</code></a> and <a href="../perlvar.html#%24%7B%5ECHILD_ERROR_NATIVE%7D"><code>${^CHILD_ERROR_NATIVE}</code></a>.</p>

</dd>
<dt id="Direct-versus-by-reference-assignment-of-filehandles"><a class="permalink" href="open.html#Direct-versus-by-reference-assignment-of-filehandles">#</a><a id="Direct"></a>Direct versus by-reference assignment of filehandles</dt>
<dd>

<p>If FILEHANDLE -- the first argument in a call to <code>open</code> -- is an undefined scalar variable (or array or hash element), a new filehandle is autovivified, meaning that the variable is assigned a reference to a newly allocated anonymous filehandle. Otherwise if FILEHANDLE is an expression, its value is the real filehandle. (This is considered a symbolic reference, so <code>use strict &quot;refs&quot;</code> should <i>not</i> be in effect.)</p>

</dd>
<dt id="Whitespace-and-special-characters-in-the-filename-argument"><a class="permalink" href="open.html#Whitespace-and-special-characters-in-the-filename-argument">#</a><a id="Whitespace"></a>Whitespace and special characters in the filename argument</dt>
<dd>

<p>The filename passed to the one- and two-argument forms of <a href="open.html"><code>open</code></a> will have leading and trailing whitespace deleted and normal redirection characters honored. This property, known as &quot;magic open&quot;, can often be used to good effect. A user could specify a filename of <i>&quot;rsh cat file |&quot;</i>, or you could change certain filenames as needed:</p>

<pre><code>$filename =~ s/(.*\.gz)\s*$/gzip -dc &lt; $1|/;
open(my $fh, $filename)
    or die &quot;Can&#39;t open $filename: $!&quot;;</code></pre>

<p>Use the three-argument form to open a file with arbitrary weird characters in it,</p>

<pre><code>open(my $fh, &quot;&lt;&quot;, $file)
	|| die &quot;Can&#39;t open $file: $!&quot;;</code></pre>

<p>otherwise it&#39;s necessary to protect any leading and trailing whitespace:</p>

<pre><code>$file =~ s#^(\s)#./$1#;
open(my $fh, &quot;&lt; $file\0&quot;)
	|| die &quot;Can&#39;t open $file: $!&quot;;</code></pre>

<p>(this may not work on some bizarre filesystems). One should conscientiously choose between the <i>magic</i> and <i>three-argument</i> form of <a href="open.html"><code>open</code></a>:</p>

<pre><code>open(my $in, $ARGV[0]) || die &quot;Can&#39;t open $ARGV[0]: $!&quot;;</code></pre>

<p>will allow the user to specify an argument of the form <code>&quot;rsh cat file |&quot;</code>, but will not work on a filename that happens to have a trailing space, while</p>

<pre><code>open(my $in, &quot;&lt;&quot;, $ARGV[0])
	|| die &quot;Can&#39;t open $ARGV[0]: $!&quot;;</code></pre>

<p>will have exactly the opposite restrictions. (However, some shells support the syntax <code>perl your_program.pl &lt;( rsh cat file )</code>, which produces a filename that can be opened normally.)</p>

</dd>
<dt id="Invoking-C-style-open"><a class="permalink" href="open.html#Invoking-C-style-open">#</a><a id="Invoking"></a>Invoking C-style <code>open</code></dt>
<dd>

<p>If you want a &quot;real&quot; C <a href="http://man.he.net/man2/open">open(2)</a>, then you should use the <a href="sysopen.html"><code>sysopen</code></a> function, which involves no such magic (but uses different filemodes than Perl <a href="open.html"><code>open</code></a>, which corresponds to C <a href="http://man.he.net/man3/fopen">fopen(3)</a>). This is another way to protect your filenames from interpretation. For example:</p>

<pre><code>use IO::Handle;
sysopen(my $fh, $path, O_RDWR|O_CREAT|O_EXCL)
    or die &quot;Can&#39;t open $path: $!&quot;;
$fh-&gt;autoflush(1);
print $fh &quot;stuff $$\n&quot;;
seek($fh, 0, 0);
print &quot;File contains: &quot;, readline($fh);</code></pre>

<p>See <a href="seek.html"><code>seek</code></a> for some details about mixing reading and writing.</p>

</dd>
<dt id="Portability-issues"><a class="permalink" href="open.html#Portability-issues">#</a><a id="Portability"></a>Portability issues</dt>
<dd>

<p>See <a href="../perlport.html#open">&quot;open&quot; in perlport</a>.</p>

</dd>
</dl>

</dd>
</dl>

</dd>
</dl>


      </div>
      <div id="footer">
        <p>Perldoc Browser is maintained by Dan Book (<a href="https://metacpan.org/author/DBOOK">DBOOK</a>). Please contact him via the <a href="https://github.com/Grinnz/perldoc-browser/issues">GitHub issue tracker</a> or <a href="mailto:dbook@cpan.org">email</a> regarding any issues with the site itself, search, or rendering of documentation.</p>

<p>The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via the <a href="https://github.com/Perl/perl5/issues">Perl issue tracker</a>, the <a href="https://lists.perl.org/list/perl5-porters.html">mailing list</a>, or <a href="https://kiwiirc.com/client/irc.perl.org/p5p">IRC</a> to report any issues with the contents or format of the documentation.</p>


      </div>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js" integrity="sha512-/DXTXr6nQodMUiq+IUJYCt2PPOUjrHJ9wFrqpJ3XkgPNOZVfMok7cRw6CSxyCQxXn6ozlESsSh1/sMCTF1rL/g==" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js" integrity="sha512-ubuT8Z88WxezgSqf3RLuNi5lmjstiJcyezx34yIU2gAHonIi27Na7atqzUZCOoY4CExaoFumzOsFQ2Ch+I/HCw==" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
    <script src="../../highlight.pack.js"></script>
    <script>hljs.highlightAll();</script>
  </body>
</html>
